package com.yungu.swift.api.route.v1.driver.pay.alipay;

import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.constants.OrderConstant;
import com.yungu.swift.order.model.vo.PayVo;
import com.yungu.swift.order.service.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Zerrion
 */
@Controller
@Api(value = "【司机】【支付】【支付宝】司机支付宝接口", tags = "司机支付宝支付的相关操作")
public class DriAliPayApi extends BaseApi {
    @Reference
    private PayService payService;

    @RequestMapping(value = "/api/v1/driver/token/pay/alipay/tradeUrl", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "支付宝支付", response = ResponseData.class, notes = "支付宝支付接口")
    public ResponseData<String> tradeUrl(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,

            @ApiParam(name = "orderUuid", value = "订单号", required = true) @RequestParam String orderUuid,
            HttpServletRequest request) {
        ResponseData<Map<String, String>> trade = payService.trade(new PayVo(OrderConstant.PAY_PURPOSE_CHARGE_FARE,
                CommonConstant.DRIVER, OrderConstant.PAY_TYPE_ALIPAY, getUserUid(request), orderUuid, null,
                null, appid, null, null));
        if (trade.isSuccess() && trade.getData() != null) {
            Map<String, String> urlMap = trade.getData();
            return ResponseData.buildSuccessResponse(urlMap.get("url"));
        }
        return ResponseData.buildErrorResponse(trade.getErrCode(), trade.getMsg());
    }

}
